package com.snail.controller;

import com.snail.dto.VideoCommentDTO;
import com.snail.result.Result;
import com.snail.service.BizCourseVideoCommentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Date 2025/8/20 1:43
 * @Author yangming
 * @Description
 **/
@RestController
@RequestMapping("/videoComment")
public class BizCourseVideoCommentController {

    @Resource
    private BizCourseVideoCommentService videoCommentService;

    @PostMapping("/addComment")
    public Result<String> addComment(@RequestBody VideoCommentDTO commentDTO) {
        videoCommentService.saveCommentToRedis(commentDTO);
        return Result.success("评论成功");
    }

    @GetMapping("/obtainComment")
    public Result<List<VideoCommentDTO>> getComments(@RequestParam("videoId") Long videoId) {
        List<Object> data=videoCommentService.getCommentsFromRedis(videoId);
        List<VideoCommentDTO> resultComment =data.stream().map(obj -> (VideoCommentDTO) obj) // 强转为DTO
                     .collect(Collectors.toList());
        return Result.success(resultComment);
    }
    @GetMapping("/getCommentCount")
    public Result<Long> getCommentCount(@RequestParam("videoId") Long videoId) {
        Long count = videoCommentService.getCommentCount(videoId);
        return Result.success(count);
    }

    @DeleteMapping("/deleteComment")
    public Result<Boolean> deleteComment(
            @RequestParam("commentId") Long commentId,
            @RequestParam("videoId") Long videoId,
            @RequestParam("userId") Long userId) {
        try {
            boolean result = videoCommentService.deleteComment(commentId, videoId, userId);
            return result ? Result.success(true) : Result.failed("删除失败");
        } catch (SecurityException e) {
            return Result.failed(e.getMessage());
        } catch (IllegalArgumentException e) {
            return Result.failed(e.getMessage());
        }
    }
}
